﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using Microsoft.Win32;

namespace ProjetGenGui
{
    /// <summary>
    /// Interaction logic for fenetrePrincipale.xaml
    /// </summary>
    public partial class fenetrePrincipale : Window
    {

        private DataTable _dt;

        public fenetrePrincipale()
        {
            _dt = new DataTable();
            _dt.Columns.Add(new DataColumn("controlePerso", typeof(String)));
            InitializeComponent();
        }

        private void ConnexionLocalCocher(object sender, RoutedEventArgs e)
        {
            gbCryptage.Visibility = System.Windows.Visibility.Visible;
        }

        private void ConnexionDistanteCocher(object sender, RoutedEventArgs e)
        {
            gbCryptage.Visibility = System.Windows.Visibility.Hidden;
        }

        private void ChoisirLesFichiers(object sender, RoutedEventArgs e)
        {
            LaDataGrid NouvelleLignePerso;
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.CheckPathExists = true;
            ofd.CheckFileExists = true;
            ofd.Multiselect = true;
            ofd.Title = "Ajouter des fichiers texte";
            ofd.Filter = "Fichiers .txt|*.txt";
            if (ofd.ShowDialog() == true)
            {
                String[] LesFichiers = ofd.FileNames;

                //1ère manière : Avec des layout
                foreach (String valeurLu in LesFichiers)
                {
                    String langueDico = "";
                    if ((Boolean)rbDicoFrancais.IsChecked)
                        langueDico = "francais";
                    else if ((Boolean)rbDicoAnglais.IsChecked)
                        langueDico = "anglais";
                    else if ((Boolean)rbDicoAll.IsChecked)
                        langueDico = "all";
                    NouvelleLignePerso = new LaDataGrid(valeurLu, (Boolean)rbDistant.IsChecked, langueDico);
                    stackListeFichiers.Children.Add(NouvelleLignePerso);
                }

                //2ème manière : avec une dataTable
                //foreach (String valeurLu in LesFichiers)
                //{
                //    //LaDataGrid nouvelleLignePerso = new LaDataGrid();
                //    String nouvelleLignePerso = "hehe";
                //    DataRow nouvelleLigne = _dt.NewRow();
                //    nouvelleLigne["controlePerso"] = nouvelleLignePerso;
                //    _dt.Rows.Add(nouvelleLigne);
                //}
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            rbDistant.IsChecked = true;
        }

        public DataTable LaDataSource
        {
            get { return _dt; }
            set { _dt = value; }
        }

        private void LancerTraitementTousFichier(object sender, RoutedEventArgs e)
        {

            Regex reg = new Regex("[0-9]{4}");
            Boolean decryptagePossible = true;
            int nbrFichier = stackListeFichiers.Children.Count;
            LaDataGrid lignePersoLu;

            try
            {
                if (!(Boolean)rbDistant.IsChecked)
                    decryptagePossible = reg.IsMatch(txtCle.Text);

                if (decryptagePossible)
                {
                    for (int i = 0; i < nbrFichier; i++)
                    {
                        lignePersoLu = (LaDataGrid)stackListeFichiers.Children[i];
                        lignePersoLu.TraitementADistance = (Boolean)rbDistant.IsChecked;
                        lignePersoLu.Cle = !(Boolean)rbDistant.IsChecked ? int.Parse(txtCle.Text) : 0;
                        lignePersoLu.LaunchTraitementFichier();
                    }
                }
            }
            catch (Exception ex)
            { 
            
            }

        }

        private void DicoFrancaisCocher(object sender, RoutedEventArgs e)
        {
            foreach (LaDataGrid ligne in stackListeFichiers.Children)
                ligne.LangueDico = "francais";
        }

        private void DicoAnglaisCocher(object sender, RoutedEventArgs e)
        {
            foreach (LaDataGrid ligne in stackListeFichiers.Children)
                ligne.LangueDico = "anglais";
        }

        private void DicoTousCocher(object sender, RoutedEventArgs e)
        {
            foreach (LaDataGrid ligne in stackListeFichiers.Children)
                ligne.LangueDico = "all";
        }

    }
}
